﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using RN.ERP.Web.BasicService;
using RN.ERP.Web.ZtbService;
using RN.ERP.Web.ModelService;




namespace RN.ERP.Web
{
    public partial class LoginExpert : BaseLoginPage
    {
        public string PageTitle
        { 
            get
            {
                return ConfigurationManager.AppSettings["sysTitle"];
            }
        }

        public string AppStyle
        {
            get
            {
                return ConfigurationManager.AppSettings["AppStyle"];
            }
        }
        public string SystemID
        {
            get
            {
                return ConfigurationManager.AppSettings["AppId"];
            }
        }
        private string _backUrl
        {
            get
            {
                return ViewState[ViewStateConstant.VIEWSTATE_BACK_URL] == null ? string.Empty : ViewState[ViewStateConstant.VIEWSTATE_BACK_URL].ToString();
            }
            set
            {
                ViewState[ViewStateConstant.VIEWSTATE_BACK_URL] = value;
            }
        }

        private Web.BasicService.BasicServiceClient basicClient = new BasicServiceClient();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                myStyleSheet.Attributes.Add("href", AppStyle + "css/style_login.css");
            }
            if(Request.QueryString[RequestConstant.REQUEST_BACK_URL] != null)
            {
                _backUrl = Request.QueryString[RequestConstant.REQUEST_BACK_URL];
            }
        }

        #region Ajax
        ////检查发送权限
        public bool CheckSendRight(string idNum,string passWord,string phoneNum)
        {
            UserInfo info = new UserInfo();
            info = JsonConvert.DeserializeObject<UserInfo>(basicClient.DoExpertLogin(idNum,passWord,SystemID, string.Empty));
            Dictionary<string, DateTime> phoneInfos = GetSessionUserInfo();
            if (info.Result)
            {
                using (ZtbService.ZtbServiceClient client = new ZtbService.ZtbServiceClient())
                {
                    var result = client.CheckSendRight(phoneNum, info.UserGuid, phoneInfos);
                    if (!phoneInfos.Keys.Contains(phoneNum))
                    {
                        phoneInfos.Add(phoneNum, DateTime.Now);//增加记录
                    }
                    bool b = result.Result;//true为有权限发送
                    if (b)
                    {
                        SendCheckCode(phoneNum);
                        Response.Write(90);
                        Response.End();
                    }
                    else
                    {
                        Response.Write(result.Message);//剩余多少秒，-1代表恶意发送
                        Response.End();
                    }
                    return b;
                }
            }
            else
            {
                Response.Write(101);
                Response.End();
                return false;
            }

        }

        private Dictionary<string,DateTime> GetSessionUserInfo()
        {
            Dictionary<string, DateTime> phoneInfos = Session[SessionConstant.SESSION_EXPERT_MOBILE] as Dictionary<string, DateTime>;
            if (phoneInfos == null)
            {
                phoneInfos = new Dictionary<string, DateTime>();
                Session[SessionConstant.SESSION_EXPERT_MOBILE] = phoneInfos;
            }
            return phoneInfos;
        }

        ////发送验证码到手机
        private void SendCheckCode(string tel)
        {
            if (!CheckPhoneCodeRight(tel))
            {
                return;
            }
            using (ZtbService.ZtbServiceClient client = new ZtbService.ZtbServiceClient())
            {
                client.GetPhoneCode(tel);
            }
        }

        private bool CheckPhoneCodeRight(string phoneNum)
        {
            if (string.IsNullOrEmpty(phoneNum))
            {
                return false;
            }
            Regex myreg = new Regex("^[1][3-8]+\\d{9}$");
            if (!myreg.IsMatch(phoneNum))
            {
                return false;
            }
            return true;
        }

        private bool CheckCodeRight(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return false;
            }
            Regex myreg = new Regex("^\\d{6}$");
            if (!myreg.IsMatch(code))
            {
                return false;
            }
            return true;
        }
        #endregion
    } 
}
